<%@page import="model.Category"%>
<%@page import="dao.DAOFactory"%>
<%@page import="java.io.File"%>
<%@page import="util.Debug"%>

<%@page import="java.util.Iterator"%>
<%@page import="model.Recipe"%>
<%@page import="java.util.List"%>

<%
@SuppressWarnings("unchecked")
List<Recipe> recipeList = (List<Recipe>) request.getAttribute("recipeListPP");
if (recipeList != null){
	Iterator<Recipe> i = recipeList.iterator();
	while(i.hasNext()) {
		Recipe recipe = i.next();
		Debug.log(request.getContextPath() + "/images/plats/" + recipe.getImage());
		
		File imageFile = new File(application.getRealPath("/images/plats") + "/" + recipe.getImage());
		String imagePath = request.getContextPath() + "/images/plats/default_plat.jpg";
		if (imageFile.isFile()) {
			imagePath = request.getContextPath() + "/images/plats/" + recipe.getImage();
		}
		
		Category category = DAOFactory.getCategoryDAO().findById(recipe.getCategory_id());
		%>
			
		<div style="clear:both">
			<div class="summary">
			<a href="<%=request.getContextPath() %>/jsp/detailsRecipe.jsp?id=<%=recipe.getId()%>&category_id=<%=recipe.getCategory_id()%>">
			<img width="125" alt="<%=recipe.getName()%>" src="<%=imagePath%>">
			</a>
			<p>
			<label class="recipeList">
			<a href="<%=request.getContextPath() %>/jsp/detailsRecipe.jsp?id=<%=recipe.getId()%>&category_id=<%=recipe.getCategory_id()%>">
			<%=category.getName() + " : " + recipe.getName()%>
			</a>
			</label>
			</p>
			<% int endIndex = recipe.getPreparation().length() >= 220 ? 220 : recipe.getPreparation().length(); %>
			 <label><%=recipe.getPreparation().subSequence(0, endIndex) + "..."%>
			  <span class="more">
			 	<a title="<%=recipe.getName()%>" href="<%=request.getContextPath() %>/jsp/detailsRecipe.jsp?id=<%=recipe.getId()%>&category_id=<%=recipe.getCategory_id()%>">Lire la suite</a>
			  </span>
			 </label>
			</div>
		</div>
		<br/>
<%
	}
}
%>